<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta name="generator" content="Hugo 0.82.1" />
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="[golang] 概念: struct vs interface#struct vs interface#go 语言的简化哲学:
class = struct &#43; receiver method set
注意:
 go 语言的struct，在参数传递中，是值拷贝。  struct 的代码示例#package main import ( &#34;fmt&#34; &#34;math&#34; ) type CircleStruct struct { x float64 y float64 r float64 } // 使用 receiver 来定义 CircleStruct 的 一个方法 func (c *CircleStruct) area() float64 { return math.Pi * c.r * c.r } // 结构体的参数的传入是一个拷贝，里面的改动不会影响原值。 func circlePlusBad(c CircleStruct) { c.">
<meta name="theme-color" content="#FFFFFF"><meta property="og:title" content="" />
<meta property="og:description" content="[golang] 概念: struct vs interface#struct vs interface#go 语言的简化哲学:
class = struct &#43; receiver method set
注意:
 go 语言的struct，在参数传递中，是值拷贝。  struct 的代码示例#package main import ( &#34;fmt&#34; &#34;math&#34; ) type CircleStruct struct { x float64 y float64 r float64 } // 使用 receiver 来定义 CircleStruct 的 一个方法 func (c *CircleStruct) area() float64 { return math.Pi * c.r * c.r } // 结构体的参数的传入是一个拷贝，里面的改动不会影响原值。 func circlePlusBad(c CircleStruct) { c." />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://example.org/docs/struct-interface/" /><meta property="article:section" content="docs" />



<title>Struct Interface | My Golang Book</title>
<link rel="manifest" href="/manifest.json">
<link rel="icon" href="/favicon.png" type="image/x-icon">
<link rel="stylesheet" href="/book.min.2bc2364a4a4b31f0ec1debecf0a8f90a840821f143dfe1ac6a0f7cbcbdcf64ac.css" integrity="sha256-K8I2SkpLMfDsHevs8Kj5CoQIIfFD3&#43;Gsag98vL3PZKw=">
<script defer src="/en.search.min.84ce9693e6c02d95376f29aeb794c11824d09ed0440f910f008e91e9ed0436e4.js" integrity="sha256-hM6Wk&#43;bALZU3bymut5TBGCTQntBED5EPAI6R6e0ENuQ="></script>
<!--
Made with Book Theme
https://github.com/alex-shpak/hugo-book
-->

  
</head>

<body dir="ltr">
  <input type="checkbox" class="hidden toggle" id="menu-control" />
  <input type="checkbox" class="hidden toggle" id="toc-control" />
  <main class="container flex">
    <aside class="book-menu">
      <div class="book-menu-content">
        
  <nav>
<h2 class="book-brand">
  <a href="/"><span>My Golang Book</span>
  </a>
</h2>


<div class="book-search">
  <input type="text" id="book-search-input" placeholder="Search" aria-label="Search" maxlength="64" data-hotkeys="s/" />
  <div class="book-search-spinner hidden"></div>
  <ul id="book-search-results"></ul>
</div>











  



  
  <ul>
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/introduction/" class="">Golang 介绍</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/installation/" class="">安装、准备开发环境</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/go-routine/" class="">并发处理 - Go 协程</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/workspace-module/" class="">管理Go模块</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/erro-handling/" class="">错误处理</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/resources/" class="">Golang 资源</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/statements/" class="">Go For语句</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/guidelines/" class="">Go 开发规范</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/logging/" class="">日志</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/data-init/" class="">Data Init</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/data-types/" class="">Data Types</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/debug/" class="">Debug</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/dynamic-programing/" class="">Dynamic Programing</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/ecosystem/" class="">Ecosystem</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/performance/" class="">Performance</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/struct-interface/" class=" active">Struct Interface</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/struct-tags/" class="">Struct Tags</a>
  

        </li>
      
    
  </ul>















</nav>




  <script>(function(){var a=document.querySelector("aside.book-menu nav");addEventListener("beforeunload",function(b){localStorage.setItem("menu.scrollTop",a.scrollTop)}),a.scrollTop=localStorage.getItem("menu.scrollTop")})()</script>


 
      </div>
    </aside>

    <div class="book-page">
      <header class="book-header">
        
  <div class="flex align-center justify-between">
  <label for="menu-control">
    <img src="/svg/menu.svg" class="book-icon" alt="Menu" />
  </label>

  <strong>Struct Interface</strong>

  <label for="toc-control">
    
    <img src="/svg/toc.svg" class="book-icon" alt="Table of Contents" />
    
  </label>
</div>


  
  <aside class="hidden clearfix">
    
  
<nav id="TableOfContents">
  <ul>
    <li><a href="#struct-vs-interface">struct vs interface</a></li>
    <li><a href="#struct-的代码示例">struct 的代码示例</a></li>
    <li><a href="#interface">interface</a></li>
  </ul>
</nav>



  </aside>
  
 
      </header>

      
      
  <article class="markdown"><h1 id="golang-概念-struct-vs-interface">
  [golang] 概念: struct vs interface
  <a class="anchor" href="#golang-%e6%a6%82%e5%bf%b5-struct-vs-interface">#</a>
</h1>
<h2 id="struct-vs-interface">
  struct vs interface
  <a class="anchor" href="#struct-vs-interface">#</a>
</h2>
<p>go 语言的简化哲学:</p>
<p><code>class = struct + receiver method set</code></p>
<p>注意:</p>
<ul>
<li>go 语言的<code>struct</code>，在参数传递中，是值拷贝。</li>
</ul>
<h2 id="struct-的代码示例">
  struct 的代码示例
  <a class="anchor" href="#struct-%e7%9a%84%e4%bb%a3%e7%a0%81%e7%a4%ba%e4%be%8b">#</a>
</h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">package</span> <span style="color:#a6e22e">main</span>

<span style="color:#f92672">import</span> (
	<span style="color:#e6db74">&#34;fmt&#34;</span>
	<span style="color:#e6db74">&#34;math&#34;</span>
)

<span style="color:#66d9ef">type</span> <span style="color:#a6e22e">CircleStruct</span> <span style="color:#66d9ef">struct</span> {
	<span style="color:#a6e22e">x</span> <span style="color:#66d9ef">float64</span>
	<span style="color:#a6e22e">y</span> <span style="color:#66d9ef">float64</span>
	<span style="color:#a6e22e">r</span> <span style="color:#66d9ef">float64</span>
}

<span style="color:#75715e">// 使用 receiver 来定义 CircleStruct 的 一个方法
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">CircleStruct</span>) <span style="color:#a6e22e">area</span>() <span style="color:#66d9ef">float64</span> {
	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">math</span>.<span style="color:#a6e22e">Pi</span> <span style="color:#f92672">*</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">r</span> <span style="color:#f92672">*</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">r</span>
}

<span style="color:#75715e">// 结构体的参数的传入是一个拷贝，里面的改动不会影响原值。
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">circlePlusBad</span>(<span style="color:#a6e22e">c</span> <span style="color:#a6e22e">CircleStruct</span>) {
	<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">x</span><span style="color:#f92672">++</span>
}

<span style="color:#75715e">// 使用 receiver 来定义 CircleStruct 的 一个方法
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">CircleStruct</span>) <span style="color:#a6e22e">circlePlusGood</span>() {
	<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">x</span><span style="color:#f92672">++</span>
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
	<span style="color:#75715e">// init: 结构体可以默认初始化
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">c</span> <span style="color:#a6e22e">CircleStruct</span>
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">c</span>)

	<span style="color:#75715e">// init: 使用 new 返回一个指针
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">c2</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">CircleStruct</span> = new(<span style="color:#a6e22e">CircleStruct</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">c2</span>)

	<span style="color:#75715e">// init: 初始化内部的字段
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">c3</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">CircleStruct</span>{<span style="color:#a6e22e">x</span>: <span style="color:#ae81ff">0</span>, <span style="color:#a6e22e">y</span>: <span style="color:#ae81ff">0</span>, <span style="color:#a6e22e">r</span>: <span style="color:#ae81ff">5</span>}
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">c3</span>)

	<span style="color:#75715e">// init: 初始化内部的字段（是用了字段的顺序）
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">c4</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">CircleStruct</span>{<span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">5</span>}
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">c4</span>)

	<span style="color:#75715e">// 调用结构的方法
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">c4</span>.<span style="color:#a6e22e">area</span>())

	<span style="color:#75715e">// 测试结构的参数传入。
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">circlePlusBad</span>(<span style="color:#a6e22e">c</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Bad, no change:&#34;</span>, <span style="color:#a6e22e">c</span>)

	<span style="color:#75715e">// 测试结构体的参数传入
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">circlePlusGood</span>()
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Good, changed:&#34;</span>,<span style="color:#a6e22e">c</span>)
}
</code></pre></div><h2 id="interface">
  interface
  <a class="anchor" href="#interface">#</a>
</h2>
<p>go 语言中的接口 <code>interface</code> 用于定义方法集。</p>
<p>结构体和接口之间是松散耦合，结构体的定义中，没有地方说明会实现什么接口。</p>
<p>在编译的过程中，将结构体的变量转为接口变量的时候，才会做检查。如果一个变量没有实现对应接口的方法，会扔出编译错误。</p>
<p>演示代码:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">package</span> <span style="color:#a6e22e">main</span>

<span style="color:#f92672">import</span> (
	<span style="color:#e6db74">&#34;fmt&#34;</span>
)

<span style="color:#75715e">// 接口的定义
</span><span style="color:#75715e"></span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">CircleInterface</span> <span style="color:#66d9ef">interface</span> {
	<span style="color:#a6e22e">area</span>() <span style="color:#66d9ef">float64</span>
}

<span style="color:#75715e">// 接口的使用
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">calcArea</span>(<span style="color:#a6e22e">c</span> <span style="color:#a6e22e">CircleInterface</span>) <span style="color:#66d9ef">float64</span> {
	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">area</span>()
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
	<span style="color:#75715e">// init: 初始化内部的字段（是用了字段的顺序）
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">c4</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">CircleStruct</span>{<span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">5</span>}

	<span style="color:#75715e">// 调用接口的方法
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">calcArea</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">c4</span>))
}
</code></pre></div></article>
 
      

      <footer class="book-footer">
        
  <div class="flex flex-wrap justify-between">





</div>

 
        
      </footer>

      
  
  <div class="book-comments">

</div>
  
 

      <label for="menu-control" class="hidden book-menu-overlay"></label>
    </div>

    
    <aside class="book-toc">
      <div class="book-toc-content">
        
  
<nav id="TableOfContents">
  <ul>
    <li><a href="#struct-vs-interface">struct vs interface</a></li>
    <li><a href="#struct-的代码示例">struct 的代码示例</a></li>
    <li><a href="#interface">interface</a></li>
  </ul>
</nav>


 
      </div>
    </aside>
    
  </main>

  
</body>

</html>












